package com.dci.service.impl.schedule;

import com.alibaba.fastjson.JSONObject;
import com.dci.context.Config;
import com.dci.dao.pms.project.PmsItemInputMapper;
import com.dci.dao.pms.schedule.PmsCapitalContractMapper;
import com.dci.dao.pms.schedule.ProgressEnteringMapper;
import com.dci.model.project.PmsItemInput;
import com.dci.model.schedule.PmsCapitalContract;
import com.dci.model.schedule.ProgressDetails;
import com.dci.model.schedule.ProgressEntering;
import com.dci.result.ResultInfo;
import com.dci.result.ResultUtil;
import com.dci.service.schedule.ProgressDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service.impl.schedule
 * @Description: 查询资金管理详情
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class ProgressDetailsServiceImpl implements ProgressDetailsService {

    @Resource
    private PmsItemInputMapper pmsItemInputMapper;

    @Resource
    private ProgressEnteringMapper progressEnteringMapper;

    @Resource
    private PmsCapitalContractMapper pmsCapitalContractMapper;

    /**
     * 查找所有资金管理数据
     *
     * @return
     */
    @Override
    public List<ProgressDetails> selectAllData(String condition) {
        JSONObject jsonObject = JSONObject.parseObject(condition);
        ProgressDetails progressDetails = JSONObject.toJavaObject(jsonObject, ProgressDetails.class);

        if (progressDetails != null) {
            if ("全部".equals(progressDetails.getProductUnit())) {
                progressDetails.setProductUnit(null);
            }
            if ("全部".equals(progressDetails.getMoneyType())) {
                progressDetails.setMoneyType(null);
            }
            if (progressDetails.getAuditStatus() == -1) {
                progressDetails.setAuditStatus(null);
            }
            if ("全部".equals(progressDetails.getUnitType())) {
                progressDetails.setUnitType(null);
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date endT = null;
        String endTime = null;
        List<PmsItemInput> list = new ArrayList<>();
        List<ProgressEntering> list1 = new ArrayList<>();

        if (progressDetails != null) {

            if (progressDetails.getEndTime() != null) {
                String eTime = progressDetails.getEndTime();
                try {
                    endT = sdf.parse(eTime);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(endT);
                    cal.add(Calendar.DATE, 1);
                    endTime = sdf.format(cal.getTime());
                } catch (ParseException e) {
                    endTime = null;
                }
            }

            PmsItemInput pms = new PmsItemInput();
            pms.setItemUnit(progressDetails.getItemUnit());
            pms.setItemName(progressDetails.getItemName());
            pms.setProductUnit(progressDetails.getProductUnit());
            pms.setAuditStatus(progressDetails.getAuditStatus());
            pms.setStartTime(progressDetails.getStartTime());
            pms.setEndTime(endTime);
            pms.setMoneyType(progressDetails.getUnitType());
            pms.setContractName(progressDetails.getContractName());
            list = pmsItemInputMapper.selectAllProject(pms);

            ProgressEntering pro = new ProgressEntering();
            pro.setProject_team(progressDetails.getItemUnit());
            pro.setName(progressDetails.getItemName());
            pro.setConstruction_unit(progressDetails.getProductUnit());
            pro.setStartTime(progressDetails.getStartTime());
            pro.setEndTime(endTime);
            pro.setAudit_status(progressDetails.getAuditStatus());
            list1 = progressEnteringMapper.selectAllProject(pro);
        } else {
            list = pmsItemInputMapper.selectAll();
            list1 = progressEnteringMapper.selectAll();
        }

        List<ProgressDetails> listDetails = new ArrayList<>();
        if (progressDetails != null && progressDetails.getUnitType() != null) {
            if ("使用资金".equals(progressDetails.getUnitType()) || "立项资金".equals(progressDetails.getUnitType())) {
                if (list.size() > 0) {
                    for (PmsItemInput pmsItemInput : list) {
                        ProgressDetails progressDetails1 = new ProgressDetails();
                        progressDetails1.setId(pmsItemInput.getId());
                        progressDetails1.setItemName(pmsItemInput.getItemName());
                        progressDetails1.setItemUnit(pmsItemInput.getItemUnit());
                        progressDetails1.setProductUnit(pmsItemInput.getProductUnit());
                        progressDetails1.setMoneyType(pmsItemInput.getMoneyType());
                        progressDetails1.setMoneyNum(pmsItemInput.getMoneyNum());
                        progressDetails1.setFileName(pmsItemInput.getFileName());
                        progressDetails1.setFileNames(pmsItemInput.getFileNames());
                        progressDetails1.setFileNum(pmsItemInput.getFileNum());
                        progressDetails1.setFileAddress(pmsItemInput.getFileAddress());
                        progressDetails1.setTime(pmsItemInput.getTime());
                        progressDetails1.setAuditStatus(pmsItemInput.getAuditStatus());
                        progressDetails1.setOpinion(pmsItemInput.getOpinion());
                        progressDetails1.setContractName(pmsItemInput.getContractName());
                        progressDetails1.setContractFile(pmsItemInput.getContractFile());

                        List<PmsCapitalContract> listContract = pmsCapitalContractMapper.selectById(pmsItemInput.getContractId());
                        if (listContract.size() > 0) {
                            for (PmsCapitalContract pmsCapitalContract : listContract) {
                                List<PmsItemInput> list2 = pmsItemInputMapper.selectByContractId(pmsCapitalContract.getId());
                                pmsCapitalContract.setList(list2);
                            }
                        }
                        pmsItemInput.setList(listContract);
                        progressDetails1.setList(pmsItemInput.getList());

                        listDetails.add(progressDetails1);
                    }
                }
            } else if ("财政资金".equals(progressDetails.getUnitType())) {
                if (list1.size() > 0) {
                    for (ProgressEntering progressEntering : list1) {
                        ProgressDetails progressDetails1 = new ProgressDetails();
                        progressDetails1.setId(progressEntering.getId());
                        progressDetails1.setItemName(progressEntering.getName());
                        progressDetails1.setItemUnit(progressEntering.getProject_team());
                        progressDetails1.setProductUnit(progressEntering.getConstruction_unit());
                        progressDetails1.setMoneyNum(Double.valueOf(progressEntering.getItem_amount()));
                        progressDetails1.setFileName(progressEntering.getProof_name());
                        progressDetails1.setFileNames(progressEntering.getProof_file_name());
                        progressDetails1.setFileNum(progressEntering.getProof_code());
                        progressDetails1.setFileAddress(progressEntering.getProof_file_path());
                        progressDetails1.setTime(progressEntering.getAmount_time());
                        progressDetails1.setAuditStatus(progressEntering.getAudit_status());
                        progressDetails1.setMoneyType(progressEntering.getSpecial_amount());

                        listDetails.add(progressDetails1);
                    }
                }
            }
        } else {
            if (list.size() > 0) {
                for (PmsItemInput pmsItemInput : list) {
                    ProgressDetails progressDetails1 = new ProgressDetails();
                    progressDetails1.setId(pmsItemInput.getId());
                    progressDetails1.setItemName(pmsItemInput.getItemName());
                    progressDetails1.setItemUnit(pmsItemInput.getItemUnit());
                    progressDetails1.setProductUnit(pmsItemInput.getProductUnit());
                    progressDetails1.setMoneyType(pmsItemInput.getMoneyType());
                    progressDetails1.setMoneyNum(pmsItemInput.getMoneyNum());
                    progressDetails1.setFileName(pmsItemInput.getFileName());
                    progressDetails1.setFileNames(pmsItemInput.getFileNames());
                    progressDetails1.setFileNum(pmsItemInput.getFileNum());
                    progressDetails1.setFileAddress(pmsItemInput.getFileAddress());
                    progressDetails1.setTime(pmsItemInput.getTime());
                    progressDetails1.setAuditStatus(pmsItemInput.getAuditStatus());
                    progressDetails1.setOpinion(pmsItemInput.getOpinion());
                    progressDetails1.setContractName(pmsItemInput.getContractName());
                    progressDetails1.setContractFile(pmsItemInput.getContractFile());

                    List<PmsCapitalContract> listContract = pmsCapitalContractMapper.selectById(pmsItemInput.getContractId());
                    if (listContract.size() > 0) {
                        for (PmsCapitalContract pmsCapitalContract : listContract) {
                            List<PmsItemInput> list2 = pmsItemInputMapper.selectByContractId(pmsCapitalContract.getId());
                            pmsCapitalContract.setList(list2);
                        }
                    }
                    pmsItemInput.setList(listContract);
                    progressDetails1.setList(pmsItemInput.getList());

                    listDetails.add(progressDetails1);
                }
            }
            if (list1.size() > 0) {
                for (ProgressEntering progressEntering : list1) {
                    ProgressDetails progressDetails1 = new ProgressDetails();
                    progressDetails1.setId(progressEntering.getId());
                    progressDetails1.setItemName(progressEntering.getName());
                    progressDetails1.setItemUnit(progressEntering.getProject_team());
                    progressDetails1.setProductUnit(progressEntering.getConstruction_unit());
                    progressDetails1.setMoneyNum(Double.valueOf(progressEntering.getItem_amount()));
                    progressDetails1.setFileName(progressEntering.getProof_name());
                    progressDetails1.setFileNames(progressEntering.getProof_file_name());
                    progressDetails1.setFileNum(progressEntering.getProof_code());
                    progressDetails1.setFileAddress(progressEntering.getProof_file_path());
                    progressDetails1.setTime(progressEntering.getAmount_time());
                    progressDetails1.setAuditStatus(progressEntering.getAudit_status());
                    progressDetails1.setMoneyType(progressEntering.getSpecial_amount());

                    listDetails.add(progressDetails1);
                }
            }
        }
        return listDetails;
    }

    @Override
    public ResultInfo deleteById(List<String> idList) {
        if (idList.size() > 0) {
            for (String id : idList) {
                pmsItemInputMapper.deleteByPrimaryKey(id);
                progressEnteringMapper.delete(id);
            }
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }

    @Override
    public ResultInfo deleteByProjectId(List<String> idList) {
        if (idList.size() > 0) {
            for (String id : idList) {
                pmsItemInputMapper.deleteByProjectId(id);
                progressEnteringMapper.deleteByProjectId(id);
            }
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }

    @Override
    public List<ProgressDetails> selectByProjectId(String projectId) {
        List<PmsItemInput> list = pmsItemInputMapper.selectByProId(projectId);
        List<ProgressEntering> list1 = progressEnteringMapper.selectByProjectId(projectId);
        List<ProgressDetails> listDetails = new ArrayList<>();
        if (list.size() > 0) {
            for (PmsItemInput pmsItemInput : list) {
                ProgressDetails progressDetails1 = new ProgressDetails();
                progressDetails1.setId(pmsItemInput.getId());
                progressDetails1.setItemName(pmsItemInput.getItemName());
                progressDetails1.setItemUnit(pmsItemInput.getItemUnit());
                progressDetails1.setProductUnit(pmsItemInput.getProductUnit());
                progressDetails1.setMoneyType(pmsItemInput.getMoneyType());
                progressDetails1.setMoneyNum(pmsItemInput.getMoneyNum());
                progressDetails1.setFileName(pmsItemInput.getFileName());
                progressDetails1.setFileNames(pmsItemInput.getFileNames());
                progressDetails1.setFileNum(pmsItemInput.getFileNum());
                progressDetails1.setFileAddress(pmsItemInput.getFileAddress());
                progressDetails1.setTime(pmsItemInput.getTime());
                progressDetails1.setAuditStatus(pmsItemInput.getAuditStatus());
                progressDetails1.setOpinion(pmsItemInput.getOpinion());
                progressDetails1.setContractName(pmsItemInput.getContractName());
                progressDetails1.setContractFile(pmsItemInput.getContractFile());

                listDetails.add(progressDetails1);
            }
        }
        if (list1.size() > 0) {
            for (ProgressEntering progressEntering : list1) {
                ProgressDetails progressDetails1 = new ProgressDetails();
                progressDetails1.setId(progressEntering.getId());
                progressDetails1.setItemName(progressEntering.getName());
                progressDetails1.setItemUnit(progressEntering.getProject_team());
                progressDetails1.setProductUnit(progressEntering.getConstruction_unit());
                progressDetails1.setMoneyNum(Double.valueOf(progressEntering.getItem_amount()));
                progressDetails1.setFileName(progressEntering.getProof_name());
                progressDetails1.setFileNames(progressEntering.getProof_file_name());
                progressDetails1.setFileNum(progressEntering.getProof_code());
                progressDetails1.setFileAddress(progressEntering.getProof_file_path());
                progressDetails1.setTime(progressEntering.getAmount_time());
                progressDetails1.setAuditStatus(progressEntering.getAudit_status());
                progressDetails1.setMoneyType(progressEntering.getSpecial_amount());

                listDetails.add(progressDetails1);
            }
        }
        return listDetails;
    }
}
